/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DBHandling.AccountHandling;
import DBHandling.BillHandling;
import DBHandling.ReturnRecieptHandling;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;

/**
 *
 * @author HuyPV
 */
public class CheckReturnedItemPanel extends javax.swing.JPanel {

    SpinnerDateModel smodelFr;
    SpinnerDateModel smodeTo;
    Vector<String> spNames;
    String to;
    String from;
    Format fm;
    String transactedBy;
    boolean ok=false;
    public CheckReturnedItemPanel() {
        initComponents();
        setOption();
    }

    private void setOption() {

        //set option spinner
        smodelFr = new SpinnerDateModel();
        smodeTo = new SpinnerDateModel();
        spnTo.setModel(smodeTo);
        spnFrom.setModel(smodelFr);

        //setDate format
        JComponent editorFr = new JSpinner.DateEditor(spnFrom, "dd MMMM yyyy");
        JComponent editorTo = new JSpinner.DateEditor(spnTo, "dd MMMM yyyy");
        spnTo.setEditor(editorTo);
        spnFrom.setEditor(editorFr);


        //load sales person
        spNames = new Vector<String>();
        try {
            spNames = new AccountHandling().getSalesPersonNames();
        } catch (Exception ex) {
        }
        //set option date
        fm = new SimpleDateFormat("MM/dd/YYYY");
        Date d1 = smodeTo.getDate();
        to = fm.format(d1);
        lblTo.setText(to);
        Date d2 = smodelFr.getDate();
        from = fm.format(d2);
        lblFrom.setText(from);
        
    }

    private void resetData() {
        lblStatus.setText("");
        lblTotal.setText("");     
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblBanner = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        spnFrom = new javax.swing.JSpinner();
        spnTo = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        btnCheckMoney = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        lblTransactedBy = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel11 = new javax.swing.JLabel();
        lblFrom = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        lblTo = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(700, 300));
        setPreferredSize(new java.awt.Dimension(747, 591));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblBanner.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/banner2.jpg"))); // NOI18N
        lblBanner.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        lblBanner.setName("lblBanner"); // NOI18N
        add(lblBanner, new org.netbeans.lib.awtextra.AbsoluteConstraints(-10, 0, 1200, 100));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Handwriting", 0, 20), new java.awt.Color(0, 0, 204))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        spnFrom.setName("spnFrom"); // NOI18N
        spnFrom.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spnFromStateChanged(evt);
            }
        });
        jPanel1.add(spnFrom, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 80, 261, -1));

        spnTo.setName("spnTo"); // NOI18N
        spnTo.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spnToStateChanged(evt);
            }
        });
        jPanel1.add(spnTo, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 140, 261, -1));

        jLabel1.setText("From");
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 80, -1, -1));

        jLabel2.setText("To");
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 140, -1, -1));

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator1.setName("jSeparator1"); // NOI18N
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(462, 30, 10, 310));

        btnCheckMoney.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ok.png"))); // NOI18N
        btnCheckMoney.setText("Check item is returned");
        btnCheckMoney.setName("btnCheckMoney"); // NOI18N
        btnCheckMoney.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCheckMoneyActionPerformed(evt);
            }
        });
        jPanel1.add(btnCheckMoney, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 200, -1, -1));

        jLabel4.setFont(new java.awt.Font("Lucida Handwriting", 0, 18));
        jLabel4.setForeground(new java.awt.Color(0, 51, 255));
        jLabel4.setText("Statistic");
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(660, 20, -1, -1));

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel6.setText("Total item is returned");
        jLabel6.setName("jLabel6"); // NOI18N
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 190, -1, 20));

        jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel7.setText("Status:");
        jLabel7.setName("jLabel7"); // NOI18N
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 380, -1, 20));

        lblTotal.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblTotal.setForeground(new java.awt.Color(255, 0, 0));
        lblTotal.setName("lblTotal"); // NOI18N
        jPanel1.add(lblTotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 190, 100, 20));

        lblTransactedBy.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblTransactedBy.setForeground(new java.awt.Color(255, 0, 0));
        lblTransactedBy.setName("lblTransactedBy"); // NOI18N
        jPanel1.add(lblTransactedBy, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 160, 150, 20));

        lblStatus.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblStatus.setForeground(new java.awt.Color(0, 0, 204));
        lblStatus.setName("lblStatus"); // NOI18N
        jPanel1.add(lblStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 380, 200, 20));

        jLabel8.setFont(new java.awt.Font("Lucida Handwriting", 0, 18));
        jLabel8.setForeground(new java.awt.Color(0, 0, 204));
        jLabel8.setText("Return day");
        jLabel8.setName("jLabel8"); // NOI18N
        jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 30, -1, 20));

        jSeparator2.setName("jSeparator2"); // NOI18N
        jPanel1.add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 370, -1, -1));

        jSeparator3.setName("jSeparator3"); // NOI18N
        jPanel1.add(jSeparator3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 360, 880, 20));

        jLabel11.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel11.setText("From");
        jLabel11.setName("jLabel11"); // NOI18N
        jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 100, -1, 20));

        lblFrom.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblFrom.setForeground(new java.awt.Color(153, 0, 153));
        lblFrom.setName("lblFrom"); // NOI18N
        jPanel1.add(lblFrom, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 100, 110, 20));

        jLabel13.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel13.setText("To");
        jLabel13.setName("jLabel13"); // NOI18N
        jPanel1.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(720, 100, 20, 20));

        lblTo.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblTo.setForeground(new java.awt.Color(153, 0, 153));
        lblTo.setName("lblTo"); // NOI18N
        jPanel1.add(lblTo, new org.netbeans.lib.awtextra.AbsoluteConstraints(760, 100, 130, 20));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 180, 950, 430));

        jButton1.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jButton1.setForeground(new java.awt.Color(255, 0, 0));
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Home-icon_32.png"))); // NOI18N
        jButton1.setText("Back to home");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 640, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void spnFromStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spnFromStateChanged
        // TODO add your handling code here:
        resetData();
        Date d2 = smodelFr.getDate();
        from = fm.format(d2);
        lblFrom.setText(from);
    }//GEN-LAST:event_spnFromStateChanged

    private void spnToStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spnToStateChanged
        // TODO add your handling code here:
        resetData();
        Date d1 = smodeTo.getDate();
        to = fm.format(d1);
        lblTo.setText(to);
    }//GEN-LAST:event_spnToStateChanged

    private void btnCheckMoneyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCheckMoneyActionPerformed
        // TODO add your handling code here:
        int total=0;
        ReturnRecieptHandling rrh = new ReturnRecieptHandling();
        
        try {
            total = rrh.getTotalItemReturned(from, to);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
        }
        
        lblTransactedBy.setText(transactedBy);
        lblTotal.setText(String.valueOf(total));
        lblStatus.setText("Completed!");

    }//GEN-LAST:event_btnCheckMoneyActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        InventoryManagement.callJPanel(new SalesManagerPanel());
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCheckMoney;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JLabel lblBanner;
    private javax.swing.JLabel lblFrom;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblTo;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JLabel lblTransactedBy;
    private javax.swing.JSpinner spnFrom;
    private javax.swing.JSpinner spnTo;
    // End of variables declaration//GEN-END:variables
}
